import api from '@/store/api';
import i18n from '@/i18n';
const { t } = i18n.global as any;

export const useAlarmSettingStore = defineStore('alarmSetting', () => {
  const snmpManagers = ref([] as any[]);
  const managersToDelete = ref([] as any[]);
  const remoteServer = ref({ Address: '', Port: 0 as string | number });

  const setSnmpMangers = (managers: {
    [x: string]: { Address: any; Port: number };
  }) => {
    snmpManagers.value = [];
    for (const key in managers) {
      snmpManagers.value.push({
        path: key,
        port: managers[key].Port,
        updatePort: false,
        address: managers[key].Address,
        updateAddress: false,
        actions: [
          {
            value: 'delete',
            title: t('pageNetwork.table.deleteDns'),
          },
        ],
      });
    }
  };
  const addNewSNMPManager = () => {
    snmpManagers.value.push({
      address: '',
      port: '',
      actions: [
        {
          value: 'delete',
          title: t('pageNetwork.table.deleteDns'),
        },
      ],
    });
  };
  const deleteManagerTableRow = (index: number) => {
    if (snmpManagers.value[index].path) {
      managersToDelete.value.push(snmpManagers.value[index].path);
    }
    snmpManagers.value.splice(index, 1);
  };
  const updateManagersSettings = async (snmpManagers: {
    [x: string]: { address: string; port: number };
  }) => {
    // Validate that no field are empty and port is valid. Port value is
    // undefined if it is an invalid number.
    for (const i in snmpManagers) {
      if (!snmpManagers[i].address || !snmpManagers[i].port) {
        //   toastService.error('Cannot save. Please resolve errors on page.');
        ElNotification({
          title: 'Error',
          message: 'Cannot save. Please resolve errors on page.',
          type: 'error',
        });
        return;
      }
    }
  };
  const getManagerData = async () => {
    return await api
      .get('/xyz/openbmc_project/network/snmp/manager/enumerate')
      .then((response) => {
        const snmpManagers = response.data.data;
        setSnmpMangers(snmpManagers);
        return snmpManagers;
      })
      .catch((error) => {
        console.log(error);
      });
  };
  const onDeleteManagerTableRow = (row: any) => {
    deleteManagerTableRow(row);
  };
  const addManager = async (payload: {
    address: any;
    port: string | number;
  }) => {
    const data = JSON.stringify({ data: [payload.address, +payload.port] });
    return await api.post(
      '/xyz/openbmc_project/network/snmp/manager/action/Client',
      data,
      undefined,
    );
  };
  const deleteManager = async (path: string) => {
    const data = JSON.stringify({ data: [] });
    return await api.post(path + '/action/Delete', data, undefined);
  };
  const setManagerAddress = async (payload: { address: any; path: string }) => {
    const data = JSON.stringify({ data: payload.address });
    return await api.put(payload.path + '/attr/Address', data);
  };
  const setManagerPort = async (payload: {
    port: string | number;
    path: string;
  }) => {
    const data = JSON.stringify({ data: +payload.port });
    return await api.put(payload.path + '/attr/Port', data);
  };
  const getRemoteServer = async () => {
    await api
      .get('/xyz/openbmc_project/logging/config/remote')
      .then(({ data: { data } }) => {
        remoteServer.value.Address = data.Address;
        remoteServer.value.Port = data.Port;
      });
  };
  const setRemoteAddress = async (payload: { Address: any }) => {
    const data = JSON.stringify({ data: payload.Address });
    return await api
      .put('/xyz/openbmc_project/logging/config/remote/attr/Address', data)
      .then(() => {
        remoteServer.value.Address = payload.Address;
      });
  };
  const setRemotePort = async (payload: { Port: any }) => {
    const data = JSON.stringify({ data: payload.Port });
    return await api
      .put('/xyz/openbmc_project/logging/config/remote/attr/Port', data)
      .then(() => {
        remoteServer.value.Port = payload.Port;
      });
  };
  const deleteServer = async () => {
    const payload = { Address: '', Port: 0 };
    setRemoteAddress(payload);
    setRemotePort(payload);
  };
  return {
    snmpManagers,
    managersToDelete,
    remoteServer,
    setSnmpMangers,
    addNewSNMPManager,
    deleteManagerTableRow,
    getManagerData,
    onDeleteManagerTableRow,
    updateManagersSettings,
    addManager,
    deleteManager,
    setManagerAddress,
    setManagerPort,
    getRemoteServer,
    setRemoteAddress,
    setRemotePort,
    deleteServer,
  };
});
